﻿using System;
using System.Drawing;
using System.Xml;
using DirectUI.Mobile.UI.Controls;
using FrameWork.Classes;
using FrameWork.Classes.Data;
using FrameWork.Event;
using FrameWork.InterFace;

namespace FrameWork.Controls
{
    public class Page : ScrollPanel, IPage
    {
        private DirectUIProgressBar progressBar;
        public string Title
        {
            get;
            set;
        }
        public DirectUIButton BackButton
        {
            get;
            set;
        }
        public void ShowProgressBar()
        {

            progressBar.Bounds = new Rectangle(0, this.Height - 20 + TopOffset, this.Width, 20);
            progressBar.Show();
        }
        public void HideProgressBar()
        {
            progressBar.Hide();
        }
        public string ProgressBarMessage
        {
            get
            {
                return progressBar.Message;
            }
            set
            {
                progressBar.Message = value;
            }
        }
        public Page(string name)
        {
            this.Name = name;
            Init();
        }
        public Page()

        {
            this.Name = Guid.NewGuid().ToString();

            Init();
        }
        private bool intialComplete = false;
        private void Init()
        {
            BeginInit();
            if (dataBindTimer == null)
            {
                dataBindTimer = new Timer();
                dataBindTimer.Enabled = true;
                dataBindTimer.Interval = 200;
            }

            this.backColor = Color.White;
            progressBar = new DirectUIProgressBar();
            progressBar.Bounds = new Rectangle(0, this.Height - 20, this.Width, 20);
            progressBar.Visible = false;
            this.Controls.Add(progressBar);
            intialComplete = true;
            modalProgress = ModalAnimateProgress.Instance;
            ShowModalProgress = true;
            //    this.Add(modalProgress);
            //     _panel.PaintBackground += new EventHandler<DirectUIPaintEventArgs>(_panel_PaintBackground);
            EndInit();
        }
        public virtual void ParseParameters(XmlNode node)
        {


        }
        protected ModalAnimateProgress modalProgress;
        protected override void OnSizeChanged(Size oldSize, Size newSize)
        {
            base.OnSizeChanged(oldSize, newSize);
            if (intialComplete)
            {
                progressBar.Bounds = new Rectangle(0, this.Height - 20, this.Width, 20);
            }

        }
        public override void OnDown(PointEventArgs e)
        {
            base.OnDown(e);
            if (this.Host != null)
            {
                if (this.Host.FocusedControl == null)
                {
                    DirectUITextBox.InputPanel.Enabled = false;

                }
                else if (this.Host.FocusedControl.GetType() != typeof(DirectUITextBox))
                {
                    DirectUITextBox.InputPanel.Enabled = false;

                }
            }
        }
        public DirectUIControlCollection Components
        {
            get
            {
                return this.controls;
            }
        }
        protected static Timer dataBindTimer;
        private bool isPaint = false;
        public override void OnEnter(IHost host)
        {
            base.OnEnter(host);
            dataBindTimer.Tick += new EventHandler(DataBindTimer_Tick);
        }
        public override void OnLeave(IHost host)
        {
            base.OnLeave(host);
            dataBindTimer.Tick -= DataBindTimer_Tick;

        }
        public override void OnLostFocus()
        {
            base.OnLostFocus();
        }

        private delegate void TimerBind(object sender, EventArgs e);
        void DataBindTimer_Tick(object sender, EventArgs e)
        {
            if (this.Host != null)
            {

                if (isPaint)
                {
                    if (DirectUIHost.Instance.InvokeRequired)
                    {
                        DirectUIHost.Instance.Invoke(new TimerBind(DataBindTimer_Tick), new object[] { sender, e });
                        return;
                    }
                    DataBind();

                    dataBindTimer.Tick -= DataBindTimer_Tick;



                }
            }
        }
        public bool ShowModalProgress { get; set; }

        public override void OnDataSourceChanged(object sender, object e)
        {
            base.OnDataSourceChanged(sender, e);
            DataSourceProvider dataSource = (DataSourceProvider)sender;
            if (dataSource.State == DataSourceState.Error)
            {
                if (modalProgress.Parent != null && ShowModalProgress)
                {
                    modalProgress.Stop();
                    //     md.Message = "网络连接出错，请检查网络，错误代码1001";
                }
#if DEBUG

                //  md.Message = dataSource.LastError;

#endif
                MessageDialog.Show("网络连接出错，请检查网络，错误代码1001", "", null);
                //if (md.Parent == null)
                //{
                //       md.OkText="";
                //    md.CancelText=null;
                //    md.ShowModal(ShowTransition.FromBottom);

                //}
                //  MessageDialog.Show(dataSource.LastError, "确定", null);

            }
            else if (dataSource.State == DataSourceState.Ready && ShowModalProgress)
            {
                if (modalProgress.Parent != null)
                {
                    modalProgress.Stop();
                }
            }
            else
            {
                if (modalProgress.Parent == null && ShowModalProgress)
                {
                    modalProgress.Start();
                }
            }

        }
        public override void OnPaint(DirectUIPaintEventArgs e)
        {
            //try
            //{
            base.OnPaint(e);

            isPaint = true;
            //}
            //catch (System.Exception ex)
            //{

            //}
        }

        //protected override onl
        public void Add(DirectUIControl c)
        {
            foreach (DirectUIControl cl in this.Controls)
            {
                if (cl.Name == c.Name)
                {
                    throw new Exception.ControlNameNotUniqueInOnePageException(c.Name);
                }
            }
            this.Controls.Add(c);

        }
        public DirectUIControl FindControl(string name)
        {
            foreach (DirectUIControl cl in this.Controls)
            {
                if (cl.Name == name)
                {
                    return cl;
                }
                else if ((cl is ControlContainer) && !(cl is DirectUIComboBox))
                {
                    foreach (DirectUIControl ccl in ((ControlContainer)cl).Controls)
                    {
                        if (ccl.Name == name)
                        {
                            return ccl;
                        }
                    }
                }

            }
            return null;
        }
        public string PrevPage { get; set; }
        public string NextPage { get; set; }
        public event EventHandler Load;
        public virtual void OnLoad(EventArgs e)
        {
            if (this.Width == 0)
            {
                this.Bounds = new Rectangle(0, 0, 240, 268);
            }
            if (Load != null)
            {
                Load(this, e);
            }
        }

    }
}
